﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using log4net;
using System.Threading;

namespace Pre.CRM
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Agent", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );
        }

        protected void Application_Start()
        {
            log4net.Config.XmlConfigurator.Configure();

            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }

        protected void Application_Error(object sender, EventArgs e)
        {
            Exception ex = Server.GetLastError();
            if (ex is ThreadAbortException) return;
            ////if (ex.Message.Contains("could not be found in the mapping and metadata information"))
            ////{
                VIT.DataHelper.LinqHelper.Global.ResetMetaData();
                Server.ClearError();
                Response.Redirect("/");
            ////}

            log.Error(ex.Message, ex);
            ////Server.ClearError();
            ////Response.Redirect("/");

            ////HttpException httpException = ex as HttpException;

            ////if (httpException != null)
            ////{
            ////    string action;

            ////    switch (httpException.GetHttpCode())
            ////    {
            ////        case 404:
            ////            // page not found
            ////            action = "HttpError404";
            ////            break;
            ////        case 500:
            ////            // server error
            ////            action = "HttpError500";
            ////            break;
            ////        default:
            ////            action = "General";
            ////            break;
            ////    }
            ////}
        }
    }
}